package com.system.dao.impl;

import com.system.config.Jdbc;
import com.system.dao.Notification;
import com.system.untils.FormatOutput;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

public class NotificationMapper implements Notification {
    private final Jdbc jb;
    private final FormatOutput fo = new FormatOutput();

    public NotificationMapper() {
        this.jb = new Jdbc();
    }

    @Override
    public void searchAll(int currentPage, String email) {
        try {
            int pageSize = 5;
            int offset = (currentPage - 1) * pageSize;  // 计算查询的偏移量
            // id->当前评论  parent_id -->
            String sql = "SELECT * FROM comments WHERE  email = ? and unread = 0 LIMIT ? OFFSET ?";
            PreparedStatement pstmt = jb.prepareStatement(sql);
            pstmt.setString(1,email);
            pstmt.setInt(2, pageSize);  // 每页显示 5 条
            pstmt.setInt(3, offset);    // 偏移量

            ResultSet rs = pstmt.executeQuery();
            int index = offset;
            while (rs.next()) {
                String name = rs.getString("userName");
                String text = rs.getString("content");
                String time = rs.getString("timestamp");
                // 根据 text 长度显示内容
                if (text.length() > 20) {
                    System.out.println("------------------------------------------------------------");
                    System.out.println(" "+(++index) +"  "+name+":");
                    System.out.println(" "+ fo.formatStringByLength(text, 20)+"\t-->"+time);
                    System.out.println("------------------------------------------------------------");
                } else {
                    System.out.println("------------------------------------------------------------");
                    System.out.println(" "+(++index) +"  "+name+":");
                    System.out.println(" "+text+"\t-->"+time);
                    System.out.println("------------------------------------------------------------");
                }
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int countUnRead(String email) {
        try {
            String sql = "SELECT * FROM comments WHERE  email = ? and unread = 0";
            PreparedStatement pstmt = jb.prepareStatement(sql);
            pstmt.setString(1,email);
            ResultSet rs = pstmt.executeQuery();
            // 处理结果集
            if (rs.next()) {
                return rs.getInt(1); // 获取查询结果中的第一列，即记录数
            }else {
                return 0;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Map<Integer, Integer> lockReplyID(String email) {
        Map<Integer, Integer> notionMap = new HashMap<>();
        try {
            int index = 0;
            String sql = "SELECT * FROM comments WHERE  email = ? and unread = 0";
            try (PreparedStatement stmt = this.jb.prepareStatement(sql)) {
                stmt.setString(1, email); // 设置查询的起始ID
                try (ResultSet rs = stmt.executeQuery()) {
                    while (rs.next()) {
                        int replyId = rs.getInt("id");
                        notionMap.put(++index, replyId);
                    }
                }
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return notionMap;
    }
    @Override
    public int lockReplyIDFroALL(int  id) {
        try {
            int index = 0;
            String sql = "SELECT * FROM comments WHERE  id = ?";
            try (PreparedStatement stmt = this.jb.prepareStatement(sql)) {
                stmt.setInt(1, id); // 设置查询的起始ID
                try (ResultSet rs = stmt.executeQuery()) {
                    if (rs.next()) {
                        return rs.getInt("publish_id");
                    }
                }
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return 0;
    }

    @Override
    public void updateRead(String email, int id) {
        try {
            Map<Integer, Integer> map = lockReplyID(email);
            String sql1 = "select unread from comments where email=? and id=?";
            PreparedStatement pstmt = this.jb.prepareStatement(sql1);
            pstmt.setString(1, email);
            pstmt.setInt(2, map.get(id));
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                if (rs.getInt("unread") == 1) {
                } else {
                    map.put(0, 0);
                    String sql2 = "update comments set unread = 1 where id = ?";
                    pstmt = this.jb.prepareStatement(sql2);
                    pstmt.setInt(1, map.get(id));
                    pstmt.execute();
                    System.out.println("已读！");
                }
            } else {
                System.out.println("no data");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);

        }
    }
    @Override
    public int getPublishID(String email)
    {
        int publishID = 0;
        try {
            String sqlGetPublishID = "select publish_id from comments where email = ?";
            PreparedStatement pstmt = jb.prepareStatement(sqlGetPublishID);
            pstmt.setString(1, email);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                publishID = rs.getInt("publish_id");
            }
        }catch (SQLException e)
        {
            throw new RuntimeException(e);
        }
        return publishID;
    }
}
